﻿using System.Web.Mvc;
using JSM;

namespace MVC4Example.Controllers
{
	public class HomeController : Controller, IJavaScriptModelAware
	{
		public ActionResult Index()
		{
			return View();
		}

		public ActionResult JavaScriptVariables()
		{
			this.AddJavaScriptVariable("SomeGlobalVariable", "This is some global variable");

			this.AddJavaScriptVariable("ExistingLiteral.SomeVariable", 42);
			this.AddJavaScriptVariable("ExistingLiteral.AnotherLiteral.SomeVariable", true);

			this.AddJavaScriptVariable("NotExistingLiteral.SomeVariable", 4.5);
			this.AddJavaScriptVariable("NotExistingLiteral.AnotherNonExistingLiteral.SomeVariable", new { TestValue = 108, TestMessage = "This is an object transfered to javascript." });


			this.AddJavaScriptVariable("ExistingLiteral.SomeInitValue", "This value has been set before ready() was fired.", PageLoadEvent.Init);

			this.AddJavaScriptFunction("JavaScriptVariablesController.Init", PageLoadEvent.Init);
			this.AddJavaScriptFunction("JavaScriptVariablesController.Ready");
			return View();
		}

		public ActionResult JavaScriptFunctions()
		{
			this.AddJavaScriptFunction("JavaScriptFunctionsController.Init", PageLoadEvent.Init);

			this.AddJavaScriptFunction("JavaScriptFunctionsController.Ready");

			this.AddJavaScriptFunction("SomeGlobalFunction", PageLoadEvent.Ready, null, "Hello :-)");
			this.AddJavaScriptFunction("JavaScriptFunctionsController.SomeFunction", PageLoadEvent.Ready, null, 42, true);
			return View();
		}
	}
}
